home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Topik / Topik - Disk 24 - Productivity (19xx)(Topik Public Domain)(PD)[WB].zip / Topik - Disk 24 - Productivity (19xx)(Topik Public Domain)(PD)[WB].adf / SpreadSheet / curses.c < prev    next >
C/C++ Source or Header  |  1990-01-31  |  7KB  |  349 lines

  1. /**********************************************************************
  2.  *
  3.  * Tiny pseudo "curses" package (runs on U__X, VMS, or MCH_AMIGA)
  4.  *
  5.  *    v1.0    870117    DBW - D. Wecker, initial hack
  6.  *
  7.  **********************************************************************/
  8.  
  9. #ifdef VMS
  10. #include <stsdef.h>
  11. #include <ssdef.h>
  12. #include <descrip.h>
  13. #include <iodef.h>
  14. #include <ttdef.h>
  15.  
  16. #define NIBUF   128        /* Input buffer size */
  17. #define NOBUF   1024        /* MM says bug buffers win! */
  18. #define EFN     0        /* Event flag */
  19.  
  20. char obuf[NOBUF];        /* Output buffer */
  21. int nobuf;            /* # of bytes in above */
  22. char ibuf[NIBUF];        /* Input buffer */
  23. int nibuf;            /* # of bytes in above */
  24. int ibufi;            /* Read index */
  25. int oldmode[2];            /* Old TTY mode bits */
  26. int newmode[2];            /* New TTY mode bits */
  27. short iochan;            /* TTY I/O channel */
  28. struct dsc$descriptor  idsc;
  29. struct dsc$descriptor  odsc;
  30. char oname[40];
  31. int iosb[2];
  32. int term[2];
  33. int status;
  34. #endif
  35.  
  36. #ifdef MCH_AMIGA
  37. extern    char        *Open();
  38. extern    long        Read(),Write();
  39. extern    void        Close();
  40. #define NEW        1006L
  41. #define AMG_MAXBUF    1024
  42. static char        *terminal = 0L;
  43. static char        scrn_tmp[AMG_MAXBUF+1];
  44. static long        scrn_tmp_p = 0L;
  45. #endif
  46.  
  47. #ifdef U__X
  48. #include <sys/ioctl.h>
  49. #include <sgtty.h>
  50. #include <stdio.h>
  51. struct sgttyb old_tty,new_tty;
  52. #endif
  53.  
  54. #ifdef MCH_AMIGA
  55. #define    COLS    79
  56. #define ROWS    23
  57. #else
  58. #define    COLS    80
  59. #define ROWS    24
  60. #endif
  61.  
  62. #define NORMAL    0x00
  63. #define BOLD    0x80
  64.  
  65. char    nscrn[ROWS][COLS],
  66.     cscrn[ROWS][COLS],
  67.     row,
  68.     col,
  69.     mode;
  70. char    str[256];
  71.  
  72. move(y,x)
  73. int y,x;
  74.     {
  75.     row = y;
  76.     col = x;
  77.     }
  78.  
  79. clrtoeol() {
  80.     int i;
  81.  
  82.     for (i = col; i < COLS; i++) nscrn[row][i] = ' ' | mode;
  83.     }
  84.  
  85. printw(fmt,a1,a2,a3,a4,a5)
  86. char    *fmt,*a1,*a2,*a3,*a4,*a5;
  87.     {
  88.     int i,j;
  89.  
  90.     sprintf(str,fmt,a1,a2,a3,a4,a5);
  91.     j = 0;
  92.     for (i = col; i < COLS && str[j] != '\000'; i++)
  93.     nscrn[row][i] = str[j++] | mode;
  94.     col = i;
  95.     }
  96.  
  97. clrtobot() {
  98.     int i,j;
  99.  
  100.     clrtoeol();
  101.     for (i = row+1; i < ROWS; i++)
  102.     for (j = 0; j < COLS; j++)
  103.         nscrn[i][j] = ' ' | mode;
  104.     }
  105.  
  106. standout() {
  107.     mode = BOLD;
  108.     }
  109.  
  110. standend() {
  111.     mode = NORMAL;
  112.     }
  113.  
  114. addstr(s)
  115. char    *s;
  116.     {
  117.     printw("%s",s);
  118.     }
  119.  
  120. initscr() {
  121.     int        i,j;
  122.  
  123. #ifdef MCH_AMIGA
  124.     terminal = Open("RAW:1/1/639/199/DBW_VC (v1.0 870117)",(long)NEW);
  125. #endif
  126. #ifdef VMS
  127.     odsc.dsc$a_pointer = "TT";
  128.     odsc.dsc$w_length = strlen(odsc.dsc$a_pointer);
  129.     odsc.dsc$b_dtype = DSC$K_DTYPE_T;
  130.     odsc.dsc$b_class = DSC$K_CLASS_S;
  131.     idsc.dsc$b_dtype = DSC$K_DTYPE_T;
  132.     idsc.dsc$b_class = DSC$K_CLASS_S;
  133.     do {
  134.     idsc.dsc$a_pointer = odsc.dsc$a_pointer;
  135.     idsc.dsc$w_length = odsc.dsc$w_length;
  136.     odsc.dsc$a_pointer = &oname[0];
  137.     odsc.dsc$w_length = sizeof(oname);
  138.     status = LIB$SYS_TRNLOG(&idsc, &odsc.dsc$w_length, &odsc);
  139.     if (status!=SS$_NORMAL && status!=SS$_NOTRAN) exit(status);
  140.     if (oname[0] == 0x1B) {
  141.         odsc.dsc$a_pointer += 4;
  142.         odsc.dsc$w_length -= 4;
  143.         }
  144.     }
  145.     while (status == SS$_NORMAL);
  146.     status = SYS$ASSIGN(&odsc, &iochan, 0, 0);
  147.     if (status != SS$_NORMAL) exit(status);
  148.     status = SYS$QIOW(EFN, iochan, IO$_SENSEMODE, iosb, 0, 0,
  149.         oldmode, sizeof(oldmode), 0, 0, 0, 0);
  150.     if (status!=SS$_NORMAL || (iosb[0]&0xFFFF)!=SS$_NORMAL) exit(status);
  151.     newmode[0] = oldmode[0];
  152.     newmode[1] = oldmode[1] | TT$M_PASSALL | TT$M_NOECHO;
  153.     status = SYS$QIOW(EFN, iochan, IO$_SETMODE, iosb, 0, 0,
  154.         newmode, sizeof(newmode), 0, 0, 0, 0);
  155.     if (status!=SS$_NORMAL || (iosb[0]&0xFFFF)!=SS$_NORMAL) exit(status);
  156. #endif
  157.  
  158. #ifdef U__X
  159.     ioctl(0,TIOCGETP,&old_tty);
  160.     ioctl(0,TIOCGETP,&new_tty);
  161.     new_tty.sg_flags |= RAW;
  162.     new_tty.sg_flags &= ~ECHO;
  163.     ioctl(0,TIOCSETP,&new_tty);
  164. #endif
  165.  
  166.     row        = 0;
  167.     col        = 0;
  168.     mode    = NORMAL;
  169.     for (i = 0; i < ROWS; i++)
  170.     for (j = 0; j < COLS; j++)
  171.         nscrn[i][j] = cscrn[i][j] = ' ';
  172.     ttputs("\033[2J");
  173.     }
  174.  
  175. clear() {
  176.     row = 0;
  177.     col = 0;
  178.     clrtobot();
  179.     }
  180.  
  181. endwin() {
  182.     move(ROWS-1,0);
  183.     refresh();
  184.  
  185. #ifdef MCH_AMIGA
  186.     amg_flush();
  187.     Close(terminal);
  188. #endif
  189.  
  190. #ifdef VMS
  191.     status = SYS$QIOW(EFN, iochan, IO$_SETMODE, iosb, 0, 0,
  192.         oldmode, sizeof(oldmode), 0, 0, 0, 0);
  193.     if (status!=SS$_NORMAL || (iosb[0]&0xFFFF)!=SS$_NORMAL) exit(status);
  194.     status = SYS$DASSGN(iochan);
  195.     if (status != SS$_NORMAL) exit(status);
  196. #endif
  197.  
  198. #ifdef U__X
  199.     ioctl(0,TIOCSETP,&old_tty);
  200. #endif
  201.  
  202.     }
  203.  
  204. char inch() {
  205.     return(nscrn[row][col] & 0x7F);
  206.     }
  207.  
  208. touchwin() {
  209.     int i,j;
  210.  
  211.     for (i=0; i<ROWS; i++)
  212.     for (j=0; j<COLS; j++)
  213.         cscrn[i][j] = ' ';
  214.     ttputs("\033[2J");
  215.     }
  216.  
  217. refresh() {
  218.     int    i,j,mode,curpos;
  219.  
  220.     mode = NORMAL;
  221.     for (i=0; i < ROWS; i++) {
  222.     curpos = -1;
  223.     for (j = 0; j < COLS; j++) {
  224.         if (nscrn[i][j] != cscrn[i][j]) {
  225.         if (curpos == -1) {
  226.             sprintf(str,"\033[%d;%dH",i+1,j+1);
  227.             ttputs(str);
  228.             curpos = j;
  229.             }
  230.         else {
  231.             sprintf(str,"\033[%dC",j-curpos);
  232.             ttputs(str);
  233.             curpos = j;
  234.             }
  235.         while (nscrn[i][j] != cscrn[i][j]) {
  236.             if (mode == NORMAL && (nscrn[i][j] & BOLD) == BOLD) {
  237.             ttputs("\033[7m");
  238.             mode = BOLD;
  239.             }
  240.             else if (mode == BOLD && (nscrn[i][j] & BOLD) == NORMAL) {
  241.             ttputs("\033[0m");
  242.             mode = NORMAL;
  243.             }
  244.             cscrn[i][j] = nscrn[i][j];
  245.             ttputc(nscrn[i][j] & 0x7F);
  246.             curpos++;
  247.             j++;
  248.             }
  249.         }
  250.         }
  251.     }
  252.     sprintf(str,"\033[%d;%dH",row+1,col+1);
  253.     ttputs(str);
  254.     if (mode) ttputs("\033[0m");
  255.     ttflush();
  256.     }
  257.  
  258. ttgetc() {
  259. #ifdef MCH_AMIGA
  260.     unsigned char ch[2];
  261.  
  262.     Read(terminal, ch, 1L);
  263.     return (ch[0] & 0xFF);
  264. #endif
  265.  
  266. #ifdef VMS
  267.     while (ibufi >= nibuf) {
  268.     ibufi = 0;
  269.     term[0] = 0;
  270.     term[1] = 0;
  271.     status = SYS$QIOW(EFN, iochan, IO$_READLBLK|IO$M_TIMED,
  272.     iosb, 0, 0, ibuf, NIBUF, 0, term, 0, 0);
  273.     if (status != SS$_NORMAL) exit(status);
  274.     status = iosb[0] & 0xFFFF;
  275.     if (status!=SS$_NORMAL && status!=SS$_TIMEOUT) exit(status);
  276.     nibuf = (iosb[0]>>16) + (iosb[1]>>16);
  277.     if (nibuf == 0) {
  278.         status = SYS$QIOW(EFN, iochan, IO$_READLBLK,
  279.         iosb, 0, 0, ibuf, 1, 0, term, 0, 0);
  280.         if (status != SS$_NORMAL || (status = (iosb[0]&0xFFFF)) != SS$_NORMAL)
  281.         exit(status);
  282.         nibuf = (iosb[0]>>16) + (iosb[1]>>16);
  283.         }
  284.     }
  285.     return (ibuf[ibufi++] & 0x7F);
  286. #endif
  287.  
  288. #ifdef U__X
  289.     return(getchar() & 0x7F);
  290. #endif
  291.     }
  292.  
  293. ttputc(c)
  294. #ifdef MCH_AMIGA
  295. char c;
  296. #endif
  297.     {
  298. #ifdef MCH_AMIGA
  299.     scrn_tmp[scrn_tmp_p++] = c;
  300.     if(scrn_tmp_p>=AMG_MAXBUF) amg_flush();
  301. #endif
  302.  
  303. #ifdef VMS
  304.     if (nobuf >= NOBUF) ttflush();
  305.     obuf[nobuf++] = c;
  306. #endif
  307.  
  308. #ifdef U__X
  309.     fputc(c, stdout);
  310. #endif
  311.     }
  312.  
  313. #ifdef MCH_AMIGA
  314. amg_flush()
  315.     {
  316.     if(scrn_tmp_p) Write(terminal,scrn_tmp,(long)scrn_tmp_p);
  317.     scrn_tmp_p = 0;
  318.     }
  319. #endif
  320.  
  321. ttputs(s)
  322. char    *s;
  323.     {
  324.     while (*s) ttputc(*s++);
  325.     }
  326.  
  327. ttflush()
  328.     {
  329. #ifdef MCH_AMIGA
  330.     amg_flush();
  331. #endif
  332.  
  333. #ifdef VMS
  334.     status = SS$_NORMAL;
  335.     if (nobuf != 0) {
  336.     status = SYS$QIOW(EFN, iochan, IO$_WRITELBLK|IO$M_NOFORMAT,
  337.     iosb, 0, 0, obuf, nobuf, 0, 0, 0, 0);
  338.     if (status == SS$_NORMAL) status = iosb[0] & 0xFFFF;
  339.     nobuf = 0;
  340.     }
  341.     return (status);
  342. #endif
  343.  
  344. #ifdef U__X
  345.     fflush(stdout);
  346. #endif
  347.     }
  348.  
  349.